<?php
namespace app\monitor\model;

//use think\Model;
use HtmlParser\ParserDom;
use app\monitor\model\CategoryInfo;

class Website 
{
    private $categoryUrls = [];
    private $url;
    private $htmlDom;

    public function __construct($url)
    {
        $this->url = $url;
        $html = file_get_contents($url);
        $this->htmlDom = new ParserDom($html); 
    }
    protected function setLastUpdateDayAttr($value)
    {
        return strtotime($value);
    }

    public function fetchCategoryUrl()
    {
        $urlDoms = $this->htmlDom->find('#mainNav li h4 a');
        foreach ($urlDoms as $urlDom) {
            //判断不包含图片或者领导内容的栏目
            if(FALSE === mb_strpos($urlDom->getPlainText(),'图片') && FALSE === mb_strpos($urlDom->getPlainText(),'领导')) {
                $cate = ['name' => $urlDom->getPlainText(),'url' => $this->url.$urlDom->href];
                $categoryUrls[] = $cate;
            }
                      
        }
        //踢掉第一个链接
        array_shift($categoryUrls);
        //return mb_stripos($categoryUrls[3]['name'],'图片');
        return $categoryUrls;
    }

    public function getCategoryInfo()
    {
        $urls = $this->fetchCategoryUrl();
        // $cate = [];
        // $cates = [];
        foreach ($urls as $url) {
           $category = new CategoryInfo($url['url']);
           $cate = [$url,'notUpdateDays'=>$category->updateToNow()];
           $cates[] = $cate;
        }
        return $cates;
    }
}